package cn.itcast.common;

/**
 * @ClassName Result
 * @Description: TODO
 * @Author lijin
 * @Date 2021/6/28
 * @Author 740393778@qq.com
 * @Version V1.0
 **/

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author gml
 * @description  全局统一返回类型
 * @date 2019/11/29
 */
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> implements Serializable {

    /**
     * 成功数据
     */
    private T data;

    /**
     * 响应编码200为成功
     */
    private Integer code;

    /**
     * 请求消耗时间
     */
    private long cost;

    /**
     * 描述
     */
    private String msg;

    /**
     * 请求id
     */
    private String requestId;

    public Result(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static Result create(Integer code, String msg){
        Result result = new Result(code, msg);
        return result;
    }

    /**
     * 无数据返回成功
     * @return
     */
    public static Result createSuccess() {
        return create(0, "操作成功");
    }

    /**
     * 有数据返回成功
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result<T> createSuccess(T data) {
        return createSuccess().setData(data);
    }

    /**
     * 无描述返回失败
     * @return
     */
    public static Result createFail() {
        return create(1, "操作失败");
    }

    /**
     * 自定义返回失败描述
     * @param code
     * @param msg
     * @return
     */
    public static Result createFail(Integer code, String msg) {
        return create(code, msg);
    }

    public Result setData(T data){
        this.data = data;
        return this;
    }

	public Integer getCode() {
		return code;
	}

	public void setCode(Integer code) {
		this.code = code;
	}

	public long getCost() {
		return cost;
	}

	public void setCost(long cost) {
		this.cost = cost;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getRequestId() {
		return requestId;
	}

	public void setRequestId(String requestId) {
		this.requestId = requestId;
	}

	public T getData() {
		return data;
	}
    
    
    

}
